import os
from SQL import SqlUtils
#表头
def show():
    print("***************学生管理系统***************")
    print("***************1：添加用户***************")
    print("***************2：查询所有用户***************")
    print("***************3：查询单个用户***************")
    print("***************4：删除用户***************")
    print("***************5：修改用户***************")
    print("***************6：退出系统***************")
#文档调入
def usetxt():
    f = open("E:/learn/实验/user/user.txt","r",encoding="utf-8")
    h = f.read()
    f.close()
    h1 = h.split("\n")
    h2 = h1[:len(h1)-1]
    userList = []
    for h3 in h2:
        h4 = eval(h3)
        userList.append(h4)
    return userList
#用户添加
def useradd():
    name = input("请输入姓名：\n")
    age = input("请输入年龄：\n")
    sex = input("请输入性别：\n")
    address = input("请输入地址：\n")
    user = {}
    user["name"] = name
    user["age"] = age
    user["sex"] = sex
    user["address"] = address
    userList.append(user)
    f = open("E:/learn/实验/user/user.txt","a",encoding="utf-8")
    f.write(str(user)+'\n')
    f.close()
#全查
def selectUserall():
    if len(userList) > 0:
        print("姓名\t\t性别\t\t年龄\t\t地址")
        for user in userList:
            print("%s\t\t%s\t\t%s\t\t%s" % (user["name"], user["sex"], user["age"], user["address"]))
    else:
        print("暂无信息可查")
#用户查找
def findUser():
    name1 = input("请输入要操作的用户姓名\n")
    u = {}
    for user in userList:
        if user["name"] == name1:
            u = user
    return u
#单查
def selectUser():
    if u == {}:
        print("没有这个人")
    else:
        print("姓名\t\t性别\t\t年龄\t\t地址")
        print("%s\t\t%s\t\t%s\t\t%s" % (u["name"], u["sex"], u["age"], u["address"]))
#用户删除
def userdel():
    if u == {}:
        print("没有这个人")
    else:
        userList.remove(u)
        print("删除成功")
        f = open("E:/learn/实验/user/user.txt", "w", encoding="utf-8")
        for p in userList:
            f.write(str(p) + "\n")
        f.close()
#用户更改
def userupdate():
    if u == {}:
        print("没有这个人")
    else:
        u["name"] = input("请输入要修改的姓名：\n")
        u["age"] = input("请输入要修改的年龄：\n")
        u["sex"] = input("请输入要修改的性别：\n")
        u["address"] = input("请输入要修改的地址：\n")
        print("修改成功")
        f = open("E:/learn/实验/user/user.txt", "w", encoding="utf-8")
        for p in userList:
            f.write(str(p) + "\n")
        f.close()

if __name__ == '__main__':
    u = SqlUtils(host="localhost",port=3306,user="root",passwd="root",db="users",charset="utf8")
    show()
    userList = []
    while True:
        num = int(input("请根据提示输入对应的编号：\n"))
        # 添加用户
        if num == 1:
            useradd()
        # 查询所有用户
        elif num == 2:
            userList = usetxt()
            selectUserall()#查询所有用户
        # 查询单个用户
        elif num == 3:
            userList = usetxt()
            u = findUser()#查找有无用户
            selectUser()
        # 删除用户
        elif num == 4:
            userList = usetxt()
            u = findUser()#查找有无用户
            userdel()
        # 修改用户
        elif num == 5:
            userList = usetxt()
            u = findUser()#查找有无用户
            userupdate()
        #结束系统
        elif num == 6:
            print("感谢使用本系统")
            break
        #输入有错误
        else:
            print("输入有误，请重新输入")